﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using JUtil;

namespace CodeGen
{
    public class DalProperty
    {
        public Type Type;
        public string Name;
        public string Comment;
    }

    public class DalFile : CodeGenBase, ICSharpFile
    {
        private readonly string NAMESPACE;
        private readonly string CLASS;
        private readonly List<DalProperty> lstDalField;

        public DalFile(string NAMESPACE, string CLASS, List<DalProperty> lstDalField)
        {
            this.NAMESPACE = NAMESPACE;
            this.CLASS = CLASS;
            this.lstDalField = lstDalField;
        }

        public DalFile(
            string NAMESPACE, 
            string CLASS, 
            string SelectSQL,
            IDatabase Database)
        {
            this.NAMESPACE = NAMESPACE;
            this.CLASS = CLASS;
            this.lstDalField = GetDalFields(SelectSQL, Database);
        }

        private List<DalProperty> GetDalFields(
            string SelectSQL, 
            IDatabase Database)
        {
            List<DalProperty> result = new List<DalProperty>();

            List<FieldSchema> lstFieldSchema = FieldSchema.GetTableSchema(Database, SelectSQL);
            foreach (FieldSchema field in lstFieldSchema)
            {
                result.Add(new DalProperty
                {
                    Type = field.DataType,
                    Name = field.ColumnName,
                    Comment = field.ColumnName
                });
            }

            return result;
        }
        #region ICSharpFile

        public string USING_NAMESPACES
        {
            get
            {
                List<string> lstUsingNameSpace = new List<string>
                {
                    "System",
                    "System.Collections.Generic"
                };

                StringBuilder output = new StringBuilder();

                foreach (string ns in lstUsingNameSpace)
                {
                    output.AppendFormat("using {0};\n", ns);
                }

                return output.ToString();
            }
        }

        public string GetNameSpace() { return NAMESPACE; }

        public string GetClass() { return CLASS; }

        private string PROPERTIES
        {
            get
            {
                StringBuilder properties = new StringBuilder();

                string propertyTemplate = GetTemplate("Property");

                foreach (DalProperty info in lstDalField)
                {
                    Hashtable variables = new Hashtable();
                    variables["TYPE"] = TypeConvertor.ToCSharpType(info.Type);
                    variables["PROPERTYNAME"] = info.Name;

                    string property = ReplaceBlock(propertyTemplate, variables);

                    properties.AppendFormat("{0}\n", property);
                }

                return properties.ToString();
            }
        }

        #endregion
        

        protected override void AssignExportTemplate(Hashtable Variables)
        {
            Variables["USING_NAMESPACES"] = USING_NAMESPACES;
            Variables["NAMESPACE"] = GetNameSpace();
            Variables["CLASS"] = GetClass();
            Variables["PROPERTIES"] = PROPERTIES;
        }

        protected override string GetExportTemplate()
        {
            return GetTemplate("ExportTemplate");
        }

    
    } // end of AnotherDalFile
}
